const app = getApp();
const api = require('../../utils/api');

//初始化预约时间可选范围
let now = new Date(); //现在时间
let year = now.getFullYear();
let month = now.getMonth() + 1;
let day = now.getDate();
let startDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day); //格式化日期

Page({
    data: {
        startDate: startDate,
        sortTimeIndex: 0,
        sortTimeList: [
            {id: 1, title: '今天'},
            {id: 2, title: '本周'},
            {id: 3, title: '本月'},
            {id: 4, title: '最近3月'},
            {id: 5, title: '最近半年'},
            {id: 6, title: '今年'},
            {id: 7, title: '自定义'},
        ],
        searchBeginTime: '',
        searchEndTime: '',
        beginTime: '',
        endTime: '',

        saleChannelList: [
            {id: 0, title: '微信小程序'},
            {id: 1, title: '商家/供应商'},
            {id: 2, title: '直播平台'},
        ],
        saleChannel: '',

        dataList: [],
        dataShowList: [], //展示用
        orderSummary: 0,
        priceSummary: 0,
        couponSummary: 0,
        noContent: false,
        isSortDone: false, //是否完成条件选择
    },
    onLoad(options) {
        this.setData({
            beginTime: this.data.startDate,
            endTime: this.data.startDate,
        });
        // this.getData();
    },
    onShow() {

    },

    getData() {
        let params = {
            saleChannel: this.data.saleChannel,
            beginTime: this.data.beginTime + ' 00:00:00',
            endTime: this.data.endTime + ' 23:59:59',
        }
        api.post('/merchant/data/base/stat', params)
            .then(res => {
                this.setData({
                    dataList: res,
                    dataShowList: this.filterData(res),
                    noContent: res.length === 0 ? true : false,
                    isSortDone: true,
                });
                // console.log(this.data.dataList)
                this.computeSummary();
            })
    },
    filterData(arr) { //整理显示的数据
        let list = app.objDeepCopy(arr);
        list.sort((a, b) => {
            return a.isMemberPay - b.isMemberPay;
        });
        
        let newList = []; //按状态分组
        for(let i = 0; i < 4; i++) {
            newList.push({
                id: i + 1,
                orderStatus: i + 1,
                data: []
            });
            let orderStatus = i + 1;

            list.forEach(item => {
                if(item.orderStatus == orderStatus && item.afterSalesStatus == 0) {
                    newList[i].data.push(item)
                }
            });
        }
        
        let afterSaleList = []; //已退款
        afterSaleList.push({
            id: -1,
            afterSalesStatus: 4,
            data: []
        });
        for(let i = 0; i < 1; i++) {
            let orderCount = 0;
            let payableAmount = 0;
            let couponAmount = 0;
            list.forEach(item => {
                if(item.isMemberPay == 0 && item.afterSalesStatus == 4) {
                    orderCount = orderCount + item.orderCount;
                    payableAmount = payableAmount + item.payableAmount;
                    couponAmount = couponAmount + item.couponAmount;
                }
            });
            afterSaleList[0].data.push({
                isMemberPay: i,
                afterSalesStatus: 4,
                orderCount: orderCount,
                payableAmount: payableAmount,
                couponAmount: couponAmount,
            });
        }

        let dealList = []; //整理后数据（过滤无数据的状态）
        newList.forEach(item => {
            if(item.data.length) {
                dealList.push(item);
            }
        });
        
        return [...dealList, ...afterSaleList];
    },
    computeSummary() { //计算总数
        let orderSummary = 0;
        let priceSummary = 0;
        let couponSummary = 0;
        this.data.dataList.forEach(item => {
            orderSummary = orderSummary + item.orderCount;
            priceSummary = priceSummary + item.payableAmount;
            if(item.isMemberPay) {
                couponSummary = couponSummary + item.couponAmount;
            }
        });
        this.setData({
            orderSummary: orderSummary,
            priceSummary: priceSummary.toFixed(2),
            couponSummary: couponSummary.toFixed(2),
        });
    },

    chooseTime(e) { //时间快捷选项
        this.setData({
            sortTimeIndex: e.currentTarget.dataset.id,
        });
        this.dealTime();
    },
    dealTime() { //整理时间
        let time = new Date();
        if(this.data.sortTimeIndex == 1) {
            //***今天
            this.setData({
                beginTime: this.data.startDate,
                endTime: this.data.startDate,
            });
        } else if(this.data.sortTimeIndex == 2) {
            //***本周
            let week = time.getDay(); //获取当前是星期几  0是星期天
            let minus = week ? week - 1 : 6; //今天距离周一的天数
            time.setDate(time.getDate() - minus); //获取minus天前的日期
            
            let y = time.getFullYear();
            let m = time.getMonth() + 1;
            let d = time.getDate();
            let start = y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d); //格式化日期

            this.setData({
                beginTime: start,
                endTime: startDate
            });
        } else if(this.data.sortTimeIndex == 3) {
            //***本月
            let y = time.getFullYear();
            let m = time.getMonth() + 1;
            let start = y + '-' + (m < 10 ? '0' + m : m) + '-' + '01'; //本月第一天日期

            this.setData({
                beginTime: start,
                endTime: startDate
            });
        } else if(this.data.sortTimeIndex == 4) {
            //***最近3个月（90天）
            time.setTime(time.getTime() - 3600 * 1000 * 24 * 90);
            let y = time.getFullYear();
            let m = time.getMonth() + 1;
            let d = time.getDate();
            let start = y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d); //格式化日期

            this.setData({
                beginTime: start,
                endTime: startDate
            });
        } else if(this.data.sortTimeIndex == 5) {
            //***最近半年（180天）
            time.setTime(time.getTime() - 3600 * 1000 * 24 * 180);
            let y = time.getFullYear();
            let m = time.getMonth() + 1;
            let d = time.getDate();
            let start = y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d); //格式化日期

            this.setData({
                beginTime: start,
                endTime: startDate
            });
        } else if(this.data.sortTimeIndex == 6) {
            //***今年
            let y = time.getFullYear();
            this.setData({
                beginTime: y + '-01-01',
                endTime: startDate
            })
        } else if(this.data.sortTimeIndex == 7) {
            return;
        }
    },
    bindDateChoose(e) { //选择自定义日期
        let item = e.currentTarget.dataset.item;
        this.setData({
            [item]: e.detail.value,
        });
    },
    chooseSalesChannel(e) { //选择销售渠道
        this.setData({
            saleChannel: e.currentTarget.dataset.id,
        });
    },

    submitSearch() { //提交搜索
        if(this.data.sortTimeIndex == 0) {
            wx.showToast({
                title: '请选择统计时间！',
                icon: 'none'
            });
            return;
        }
        if(this.data.sortTimeIndex == 7) {
            if(!this.data.searchBeginTime) {
                wx.showToast({
                    title: '请选择搜索开始日期！',
                    icon: 'none'
                });
                return;
            }
            if(!this.data.searchEndTime) {
                wx.showToast({
                    title: '请选择搜索结束日期！',
                    icon: 'none'
                });
                return;
            }
            let beginTime = new Date(this.data.searchBeginTime);
            let endTime = new Date(this.data.searchEndTime);
            if(beginTime > endTime) {
                wx.showToast({
                    title: '开始日期不能大于结束日期！',
                    icon: 'none'
                });
                return;
            }
            this.setData({
                beginTime: this.data.searchBeginTime,
                endTime: this.data.searchEndTime,
            });
        }
        if(this.data.saleChannel === '') {
            wx.showToast({
                title: '请选择销售渠道！',
                icon: 'none'
            });
            return;
        }
        this.getData();
    },

    onPullDownRefresh() {

    },
    onReachBottom() {

    },
})